<?php
/**
 * soap服务端
 * 宁东服务器地址：http://10.217.84.158:28080/services/OutBoundService?wsdl
 * 正式环境地址：http://10.217.2.232:80/AQGQJ_Trace_AQFXGK/ps/SoapServicePHPPort_ps?wsdl
 */

namespace App\Services;
use App\Http\Controllers\Admin\ThirdController;
use http\Env\Request;

Class SoapService
{
    public function sendOutBoundData($parameters)
    {
        $getXml = Request()->getContent(); //file_get_contents("php://input");https://zhuanlan.zhihu.com/p/510814673
        info('风控请求xml参数:'.$getXml);
        try {
            //该转义的转义
            $getXml = str_replace(array("\r","\n"),'',$getXml);
            $getXml = str_replace('&lt;','<',$getXml);
            $getXml = str_replace('&gt;','>',$getXml);
            $getXml = str_replace('&#13;','',$getXml);
            //处理xml字符串
            $dom = new \DOMDocument();
            $dom->loadXML($getXml);
            $root = $dom->documentElement;
            $content = $dom->getElementsByTagName('param');
            $cdata = $content->item(0)->nodeValue;
            $Toolxml = json_encode(simplexml_load_string($cdata));
            $Toolxml = json_decode($Toolxml,true);
            $tool_list = $Toolxml['items']['item'];
            if(!$tool_list){
                $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
                $result_str .= '解析后数据为空！</items></resultValue>';
                $result_str .= '<successful type="boolean">false</successful></o>';
                return array('return'=>$result_str);
            }
        }catch(\Exception $e) {
            $msg = "Exception error :" . $e->getMessage().' Line:'.$e->getLine();
            info($msg); //写入日志
            $result_str ='<?xml version="1.0" encoding="UTF-8"?><o><resultValue><items class="array">';
            $result_str .= $msg.'</items></resultValue>';
            $result_str .= '<successful type="boolean">false</successful></o>';
            return array('return'=>$result_str);
        }
        //存储获取到的参数
        info('解析后的param元素:'.json_encode($Toolxml));
        $third = new \App\Http\Controllers\Admin\ThirdController();
        //判断解析出来的是否为二维，不是的话进行构造
        $tool_list_arr = [];
        if(isset($tool_list['prid'])){
            $tool_list_arr[] = $tool_list;
        }else{
            $tool_list_arr = $tool_list;
        }
        $saveRes =  $third->autoData($tool_list_arr);
        //方法一：使用simplexml_load_string直接转换拼装好的字符串
        //$xmlObject = simplexml_load_string($saveRes);
        //$saveRes = $xmlObject->asXML();
        return array('return'=>$saveRes);

    }
}
?>
